// pages/myscore/index.js
import urls from '../../utils/urls.js'
import loginFn from '../../utils/login.js'
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		myscore: 0,
		types: 'all', //tab类型
		tabIdx: 1,
		list: [],
		totalPage: 1,
		page: 1,
		perpage: 20
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		console.log('options:',options);
		var that = this;
		if (options.score) {
			this.setData({
				myscore: options.score
			})
		};
		var loginInfo = loginFn()
		loginInfo.type = 'all'
		wx.request({
			url: urls.scorelist,
			method: 'POST',
			data: loginInfo,
			success: function(res) {
				if (res.data.code == '0') {
					that.setData({
						list: res.data.data.list,
						totalPage: res.data.data.totalPage
					})
				}
			}
		})
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {
		if (this.data.page < this.data.totalPage) {
			var loginInfo = loginFn()
			loginInfo.type = e.currentTarget.dataset.type
			wx.request({
				url: urls.scorelist,
				method: 'POST',
				data: loginInfo,
				success: function(res) {
					if (res.data.code == '0') {
						var list = res.data.data.list
						var alllist = that.data.list.concat(list)
						that.setData({
							list: alllist,
							page: this.data.page + 1
						})
					}
				}
			})
		}

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	},
	tabFn: function(e) { //tab切换
		console.log(e)
		var that = this;
		this.setData({
			tabIdx: e.currentTarget.dataset.idx,
			types: e.currentTarget.dataset.type
		})
		var loginInfo = loginFn()
		loginInfo.type = e.currentTarget.dataset.type
		wx.request({
			url: urls.scorelist,
			method: 'POST',
			data: loginInfo,
			success: function(res) {
				if (res.data.code == '0') {
					that.setData({
						list: res.data.data.list,
						totalPage: res.data.data.totalPage,
						page: 1
					})
				}
			}
		})
	},
	agentFn: function(e) { //上报获取积分
		wx.navigateTo({
			url: '/pages/addinstitution/index',
		})
	},
})
